package cc.linker.steplink.crm.merchant.service.impl;

import cc.linker.steplink.crm.commons.BeanMapUtils;
import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.merchant.dao.NewMallDao;
import cc.linker.steplink.crm.merchant.dao.acs.MerchantInfoDao;
import cc.linker.steplink.crm.merchant.dao.integral.IntegralGoodsDao;
import cc.linker.steplink.crm.merchant.pojo.integral.IntegralGoodsPo;
import cc.linker.steplink.crm.merchant.pojo.integral.IntegralGoodsSpec;
import cc.linker.steplink.crm.merchant.pojo.integral.IntegralSku;
import cc.linker.steplink.crm.merchant.service.integral.IntegralGoodsService;
import cc.linker.steplink.crm.merchanth5.dao.VipCustomerLogDao;
import cc.linker.steplink.crm.merchanth5.pojo.VipPo;
import cc.linker.steplink.crm.merchanth5.service.acs.DynamicService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IntegralGoodsServiceImpl implements IntegralGoodsService {

    @Autowired
    private IntegralGoodsDao integralGoodsDao;
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private CommonsService commonsService;
    @Autowired
    private VipCustomerLogDao vipCustomerLogDao;
    @Autowired
    private MerchantInfoDao merchantInfoDao;
    @Autowired
    private NewMallDao newMallDao;

    /**
     * 商品列表
     *
     * @param ：pageIndex   页码
     * @param ：pageSize    页面大小
     * @param ：goodsstatus 状态
     * @param ：key         关键字
     */
    @Override
    public Object getGoodsList(Map<String, Object> params) {
        int pageIndex = 1, pageSize = 20;
        Object pi = params.get("pageIndex");
        if (pi instanceof Integer && (int) pi >= 0) {
            pageIndex = (int) pi;
        }
        Object ps = params.get("pageSize");
        if (ps instanceof Integer && (int) ps >= 0) {
            pageSize = (int) ps;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Map> goodsList = integralGoodsDao.getGoodsList(params);
        for (Map goodsInfo : goodsList) {
            String quotaLevel = "";
            if (!StringUtils.isEmpty(goodsInfo.get("quotaLevel")) && !goodsInfo.get("quotaLevel").toString().equals("0")) {
                quotaLevel = "大于等于vip" + goodsInfo.get("quotaLevel").toString();
            }
            if (!StringUtils.isEmpty(goodsInfo.get("quotaVips"))) {
                String quotaVipNames1 = integralGoodsDao.getQuotaVipNames(goodsInfo.get("quotaVips").toString());
                if (!StringUtils.isEmpty(quotaLevel)) {
                    quotaLevel = quotaLevel + ";" + quotaVipNames1;
                } else {
                    quotaLevel = quotaVipNames1;
                }
            }
            goodsInfo.put("quotaVipNames", quotaLevel);
        }
        return new PageInfo<>(goodsList);
    }

    /**
     * 商品列表(小B)
     *
     * @param ：pageIndex   页码
     * @param ：pageSize    页面大小
     * @param ：goodsstatus 状态
     * @param ：key         关键字
     */
    @Override
    public Object getGoodsListByAdminId(Map<String, Object> params) {
        int pageIndex = 1, pageSize = 20;
        Object pi = params.get("pageIndex");
        if (pi instanceof Integer && (int) pi >= 0) {
            pageIndex = (int) pi;
        }
        Object ps = params.get("pageSize");
        if (ps instanceof Integer && (int) ps >= 0) {
            pageSize = (int) ps;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Map> goodsList = integralGoodsDao.getGoodsListByAdminId(params);
        for (Map goodsInfo : goodsList) {
            String quotaLevel = "";
            if (!StringUtils.isEmpty(goodsInfo.get("quotaLevel")) && !goodsInfo.get("quotaLevel").toString().equals("0")) {
                quotaLevel = "大于等于vip" + goodsInfo.get("quotaLevel").toString();
            }
            if (!StringUtils.isEmpty(goodsInfo.get("quotaVips"))) {
                String quotaVipNames1 = integralGoodsDao.getQuotaVipNames(goodsInfo.get("quotaVips").toString());
                if (!StringUtils.isEmpty(quotaLevel)) {
                    quotaLevel = quotaLevel + ";" + quotaVipNames1;
                } else {
                    quotaLevel = quotaVipNames1;
                }
            }
            goodsInfo.put("quotaVipNames", quotaLevel);
        }
        return new PageInfo<>(goodsList);
    }

    /**
     * 积分商品排序
     *
     * @param ： goodsId 商品 id
     * @param ： sort 当前商品排序
     * @param ： type 0 向下 1 向上
     */
    @Override
    public void updateSort(Long goodsId, Integer sort, Integer type, String orgId) {
        Integer oldSort = sort;
        Integer nowSort = null;
        //type 0 向下 1 向上
        Map<String, Object> param = new HashMap<>();
        param.put("sort", sort);
        param.put("type", type);
        param.put("orgId", orgId);
        //查询
        IntegralGoodsPo goodsInfo = integralGoodsDao.getGoodsBySort(param);
        if (goodsInfo == null) {
            throw GlobalException.build("无法移动");
        }
        nowSort = goodsInfo.getSort();
        //  获取当前商品的normalSort
        integralGoodsDao.updateSort(goodsInfo.getGoodsId(), oldSort);
        integralGoodsDao.updateSort(goodsId, nowSort);
    }

    /**
     * 积分商品删除
     *
     * @param ： goodsId 商品 id
     */
    @Override
    public void delete(Long goodsId) {
        integralGoodsDao.delete(goodsId);
    }

    /**
     * 积分商城规则
     */
    @Override
    public Object getrule(String orgId, Integer ruleType) {
        Map map = integralGoodsDao.getrule(orgId, ruleType);
        if (map == null) {
            map = new HashMap();
            map.put("rule", "");
            map.put("orgId", orgId);
            map.put("ruleType", ruleType);
            integralGoodsDao.insertrule(map);
        }
        return map;
    }

    /**
     * 积分商城规则保存
     *
     * @param ：rule  规则富文本
     * @param ：orgId 机构id
     */
    @Override
    public void saverule(Map<String, Object> param) {
        integralGoodsDao.updaterule(param);
    }

    @Override
    public void updateStatus(Map<String, Object> param, Boolean saler) {
        if (!saler) {
            long goodsId = Long.parseLong(param.get("goodsId").toString());
            IntegralGoodsPo goodsInfo = integralGoodsDao.getGoodsInfo(goodsId);
            if (!"".equals(goodsInfo.getBelongId())) {
                integralGoodsDao.updateOrgStatus(param);
                return;
            }
        }
        integralGoodsDao.updateStatus(param);
    }

    @Override
    public void save(Map<String, Object> params, String orgId) {
        Object goodsIdObj = params.get("goodsId");
        Integer goodsType = 1;
        if (!StringUtils.isEmpty(params.get("goodsType"))) {
            goodsType = Integer.valueOf(params.get("goodsType").toString());
        }
        if (goodsType == 1 || goodsType == 2) {
            //普通商品
            params.put("goodsType", goodsType);
            Object sku = params.get("sku");
            Integer originalGoods = 0;
            if (!StringUtils.isEmpty(params.get("originalGoodsId"))) {
                Long originalGoodsId = Long.valueOf(params.get("originalGoodsId").toString());
                originalGoods = integralGoodsDao.getOriginalGoods(originalGoodsId);
            }
            if (originalGoods == 0 || originalGoods == null) {
                throw GlobalException.build("原商品已被删除,建议更换商品！");
            }
            Long goodsId;
            if (goodsIdObj != null && !StringUtils.isEmpty(goodsIdObj)) {
                goodsId = Long.valueOf(goodsIdObj.toString());
                if (StringUtils.isEmpty(params.get("goodsStatus")) || "3".equals(params.get("goodsStatus").toString())) {
                    params.put("goodsStatus", 2);
                }
                integralGoodsDao.updateGoods(params);
                // 删除商品规格关联关系
                integralGoodsDao.deleteGoodsSpecs(goodsId);
            } else {
                params.put("goodsStatus", 2);
                integralGoodsDao.insertGoods(params);
                goodsId = Long.valueOf(params.get("goodsId").toString());
                // 新增时其他未删除状态的商品排序字段加1
                integralGoodsDao.addAllGoodsSort(goodsId, orgId);
                params.put("type", 16);
                params.put("desc", "新增" + params.get("goodsName"));
                dynamicService.recording(params);
            }
            Object goodsSpec = params.get("goodsSpec");
            if (goodsSpec instanceof List && !((List) goodsSpec).isEmpty()) {
                for (Object o : (List) goodsSpec) {
                    if (o instanceof Map) {
                        Map<String, Object> map = (Map) o;
                        map.put("goodsId", goodsId);
                        integralGoodsDao.insertGoodsSpec(map);
                    }
                }
            }
            Long totalCount = 0L;
            List<Map<String, Object>> newSku = new ArrayList<>();
            if (sku instanceof List && !((List) sku).isEmpty()) {
                List<Long> skuIds = new ArrayList<>();
                for (Object o : (List) sku) {
                    if (o instanceof Map) {
                        Map<String, Object> map = (Map) o;
                        map.put("goodsId", goodsId);
                        long newSkuId = 0L;
                        integralGoodsDao.insertSku(map);
                        if (!StringUtils.isEmpty(map.get("skuId"))) {
                            newSkuId = Long.parseLong(map.get("skuId").toString());
                            map.put("newSkuId", map.get("skuId"));
                        } else {
                            newSkuId = Long.parseLong(map.get("newSkuId").toString());
                        }
                        // 统计库存
                        long skuCount = Long.parseLong(map.get("goodsCount").toString());
                        //mallService.flushGoodsCache(false,goodsId,String.valueOf(newSkuId),skuCount);
                        // 记录 存在的sku list 用于删除不存在的sku记录
                        skuIds.add(newSkuId);
                        // 往新的sku列表加 记录
                        newSku.add(map);
                        totalCount += skuCount;
                    }
                }
                // 删除未出现的skuId
                if (skuIds.size() > 0 && !StringUtils.isEmpty(goodsId)) {
                    integralGoodsDao.delSkuBySkuIds(skuIds, goodsId);
                }
                params.put("goodsCount", totalCount);
                integralGoodsDao.updateGoods(params);
            } else {
                // 删除规格
                integralGoodsDao.deleteSkus(goodsId);
            }
        } else {
            //权益卡积分商品
            Integer originalGoods = 0;
            if (!StringUtils.isEmpty(params.get("originalGoodsId"))) {
                Long originalGoodsId = Long.valueOf(params.get("originalGoodsId").toString());
                originalGoods = integralGoodsDao.getOriginalVip(originalGoodsId);
            }
            if (originalGoods == 0 || originalGoods == null) {
                throw GlobalException.build("原权益卡已被删除,建议更换权益卡！");
            }
            String options = params.get("options").toString();
            List<Map<String, Object>> optionsList = (List) JSON.parse(options);
            Integer minIntegral = 0;
            String amount = "0";
            for (Map<String, Object> op : optionsList) {
                Integer integral = Integer.valueOf(op.get("integral").toString());
                if (minIntegral == 0 || minIntegral > integral) {
                    minIntegral = integral;
                    if (!StringUtils.isEmpty(op.get("amount"))) {
                        amount = op.get("amount").toString();
                    }
                }
            }
            params.put("integral", minIntegral);
            params.put("moneyAmount", amount);
            Long goodsId;
            if (goodsIdObj != null && !StringUtils.isEmpty(goodsIdObj)) {
                if (StringUtils.isEmpty(params.get("goodsStatus")) || "3".equals(params.get("goodsStatus").toString())) {
                    params.put("goodsStatus", 2);
                }
                integralGoodsDao.updateGoods(params);
            } else {
                params.put("goodsStatus", 2);
                integralGoodsDao.insertGoods(params);
                goodsId = Long.valueOf(params.get("goodsId").toString());
                // 新增时其他未删除状态的商品排序字段加1
                integralGoodsDao.addAllGoodsSort(goodsId, orgId);
                params.put("type", 16);
                params.put("desc", "新增" + params.get("goodsName"));
                dynamicService.recording(params);
            }
        }
    }

    @Override
    public void saveByAdminId(Map<String, Object> params, String orgId) {
        Object goodsIdObj = params.get("goodsId");
        Integer goodsType = 1;
        if (!StringUtils.isEmpty(params.get("goodsType"))) {
            goodsType = Integer.valueOf(params.get("goodsType").toString());
        }
        if (goodsType == 1 || goodsType == 2) {
            //普通商品
            params.put("goodsType", goodsType);
            Object sku = params.get("sku");
            Integer originalGoods = 0;
            if (!StringUtils.isEmpty(params.get("originalGoodsId"))) {
                Long originalGoodsId = Long.valueOf(params.get("originalGoodsId").toString());
                originalGoods = integralGoodsDao.getOriginalGoods(originalGoodsId);
            }
            if (originalGoods == 0 || originalGoods == null) {
                throw GlobalException.build("原商品已被删除,建议更换商品！");
            }
            Long goodsId;
            if (goodsIdObj != null && !StringUtils.isEmpty(goodsIdObj)) {
                goodsId = Long.valueOf(goodsIdObj.toString());
                if (StringUtils.isEmpty(params.get("goodsStatus")) || "3".equals(params.get("goodsStatus").toString())) {
                    params.put("goodsStatus", 2);
                }
                integralGoodsDao.updateGoods(params);
                // 删除商品规格关联关系
                integralGoodsDao.deleteGoodsSpecs(goodsId);
            } else {
                params.put("goodsStatus", 2);
                integralGoodsDao.insertGoodsByAdminId(params);
                goodsId = Long.valueOf(params.get("goodsId").toString());
                // 新增时其他未删除状态的商品排序字段加1
                integralGoodsDao.addAllGoodsSort(goodsId, orgId);
                params.put("type", 16);
                params.put("desc", "新增" + params.get("goodsName"));
                dynamicService.recording(params);
            }
            Object goodsSpec = params.get("goodsSpec");
            if (goodsSpec instanceof List && !((List) goodsSpec).isEmpty()) {
                for (Object o : (List) goodsSpec) {
                    if (o instanceof Map) {
                        Map<String, Object> map = (Map) o;
                        map.put("goodsId", goodsId);
                        integralGoodsDao.insertGoodsSpec(map);
                    }
                }
            }
            Long totalCount = 0L;
            List<Map<String, Object>> newSku = new ArrayList<>();
            if (sku instanceof List && !((List) sku).isEmpty()) {
                List<Long> skuIds = new ArrayList<>();
                for (Object o : (List) sku) {
                    if (o instanceof Map) {
                        Map<String, Object> map = (Map) o;
                        map.put("goodsId", goodsId);
                        long newSkuId = 0L;
                        integralGoodsDao.insertSku(map);
                        if (!StringUtils.isEmpty(map.get("skuId"))) {
                            newSkuId = Long.parseLong(map.get("skuId").toString());
                            map.put("newSkuId", map.get("skuId"));
                        } else {
                            newSkuId = Long.parseLong(map.get("newSkuId").toString());
                        }
                        // 统计库存
                        long skuCount = Long.parseLong(map.get("goodsCount").toString());
                        //mallService.flushGoodsCache(false,goodsId,String.valueOf(newSkuId),skuCount);
                        // 记录 存在的sku list 用于删除不存在的sku记录
                        skuIds.add(newSkuId);
                        // 往新的sku列表加 记录
                        newSku.add(map);
                        totalCount += skuCount;
                    }
                }
                // 删除未出现的skuId
                if (skuIds.size() > 0 && !StringUtils.isEmpty(goodsId)) {
                    integralGoodsDao.delSkuBySkuIds(skuIds, goodsId);
                }
                params.put("goodsCount", totalCount);
                integralGoodsDao.updateGoods(params);
            } else {
                // 删除规格
                integralGoodsDao.deleteSkus(goodsId);
            }
        } else {
            //权益卡积分商品
            Integer originalGoods = 0;
            if (!StringUtils.isEmpty(params.get("originalGoodsId"))) {
                Long originalGoodsId = Long.valueOf(params.get("originalGoodsId").toString());
                originalGoods = integralGoodsDao.getOriginalVip(originalGoodsId);
            }
            if (originalGoods == 0 || originalGoods == null) {
                throw GlobalException.build("原权益卡已被删除,建议更换权益卡！");
            }
            String options = params.get("options").toString();
            List<Map<String, Object>> optionsList = (List) JSON.parse(options);
            Integer minIntegral = 0;
            String amount = "0";
            for (Map<String, Object> op : optionsList) {
                Integer integral = Integer.valueOf(op.get("integral").toString());
                if (minIntegral == 0 || minIntegral > integral) {
                    minIntegral = integral;
                    if (!StringUtils.isEmpty(op.get("amount"))) {
                        amount = op.get("amount").toString();
                    }
                }
            }
            params.put("integral", minIntegral);
            params.put("moneyAmount", amount);
            Long goodsId;
            if (goodsIdObj != null && !StringUtils.isEmpty(goodsIdObj)) {
                if (StringUtils.isEmpty(params.get("goodsStatus")) || "3".equals(params.get("goodsStatus").toString())) {
                    params.put("goodsStatus", 2);
                }
                integralGoodsDao.updateGoods(params);
            } else {
                params.put("goodsStatus", 2);
                integralGoodsDao.insertGoodsByAdminId(params);
                goodsId = Long.valueOf(params.get("goodsId").toString());
                // 新增时其他未删除状态的商品排序字段加1
                integralGoodsDao.addAllGoodsSort(goodsId, orgId);
                params.put("type", 16);
                params.put("desc", "新增" + params.get("goodsName"));
                dynamicService.recording(params);
            }
        }
    }

    @Override
    public Object getGoodsInfo(Long goodsId) {
        if (goodsId == null) {
            throw GlobalException.build("商品id不能为空");
        }
        IntegralGoodsPo goodsPo = integralGoodsDao.getGoodsInfo(goodsId);
        if (goodsPo.getGoodsType() == 1 || goodsPo.getGoodsType() == 2) {
            Long originalGoodsCount = integralGoodsDao.getOriginalGoodsCount(goodsPo.getOriginalGoodsId());
            goodsPo.setOriginalCount(originalGoodsCount);
            //获取原商品sku的库存
            List<IntegralGoodsSpec> goodsSpec = integralGoodsDao.getSpecByGoodsId(goodsId);
            goodsPo.setGoodsSpec(goodsSpec);
            List<IntegralSku> sku = integralGoodsDao.getSkuByGoodsId(goodsId);
            if (sku != null && sku.size() > 0) {
                //获取原商品sku的库存
                for (IntegralSku skuInfo : sku) {
                    Long goodsSkuId = skuInfo.getGoodsSkuId();
                    Long originalCount = integralGoodsDao.getSkuCount(goodsSkuId);
                    skuInfo.setOriginalCount(originalCount);
                }
            }
            goodsPo.setSku(sku);
            Map goodsMap = BeanMapUtils.beanToMap(goodsPo);
            if (!StringUtils.isEmpty(goodsPo.getQuotaVips())) {
                List<Map> vipList = integralGoodsDao.getQuotaVipList(goodsPo.getQuotaVips());
                goodsMap.put("quotaVipList", vipList);
            }
            return goodsMap;
        } else {
            Map vip = BeanMapUtils.beanToMap(goodsPo);
            //获取原权益卡信息
            Map map = new HashMap();
            map.put("id", goodsPo.getOriginalGoodsId());
            VipPo originalVip = vipCustomerLogDao.vipInfo(map);
            if (originalVip != null) {
                vip.put("originalOptions", originalVip.getOptions());
                vip.put("originalVipType", originalVip.getType());
            }
            if (!StringUtils.isEmpty(goodsPo.getQuotaVips())) {
                List<Map> vipList = integralGoodsDao.getQuotaVipList(goodsPo.getQuotaVips());
                vip.put("quotaVipList", vipList);
            }
            return vip;
        }
    }

    @Override
    public List<Map<String, Object>> exchangelist(Map<String, Object> params) {
        List<Map<String, Object>> orderList = integralGoodsDao.exchangelist(params);
        return orderList;
    }

    @Override
    public void exportExchangelist(Map<String, Object> params, HttpServletResponse response) {
        String[] title = {"时间", "兑换人", "手机号", "兑换内容", "兑换价格"};
        String fileName = "兑换记录";
        String[] strings = {"createTime", "nickName", "phone", "goodsName", "integral"};
        List<Map<String, Object>> orderList = integralGoodsDao.exchangelist(params);
        for (Map<String, Object> order : orderList) {
            if (!StringUtils.isEmpty(order.get("createTime"))) {
                order.put("createTime", order.get("createTime").toString().substring(0, 19));
            }
            if (!StringUtils.isEmpty(order.get("integral"))) {
                order.put("integral", order.get("integral").toString() + "积分");
                if (!StringUtils.isEmpty(order.get("orderAmount")) && !"0.00".equals(order.get("orderAmount").toString())) {
                    order.put("integral", order.get("integral").toString() + "+" + order.get("orderAmount").toString() + "元");
                }
            }
        }
        try {
            commonsService.excelDownload(orderList, strings, title, fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateGoodsToIntegralGoods(Long goodsId, Map<String, Object> params, List<Map<String, Object>> newSku, Object goodsSpec) {
        //判断该商品是否有积分商品
        List<Long> integralGoodsIds = integralGoodsDao.getIntegralGoodsId(goodsId);
        if (integralGoodsIds != null && integralGoodsIds.size() > 0) {
            params.remove("goodsCount");
            for (Long integralGoodsId : integralGoodsIds) {
                //获取原来的积分商品的sku
                List<IntegralSku> skuListO = integralGoodsDao.getSkuByGoodsId(integralGoodsId);
                //删除Sku
                integralGoodsDao.deleteSkus(integralGoodsId);
                if (newSku != null && newSku.size() > 0) {
                    //删除规格
                    integralGoodsDao.deleteGoodsSpecs(integralGoodsId);
                    if (goodsSpec instanceof List && !((List) goodsSpec).isEmpty()) {
                        for (Object o : (List) goodsSpec) {
                            if (o instanceof Map) {
                                Map<String, Object> map = (Map) o;
                                map.put("goodsId", integralGoodsId);
                                integralGoodsDao.insertGoodsSpec(map);
                            }
                        }
                    }
                    //有sku可能失效
                    //查询是不是有skuId，没有的话就失效
                    Integer isAble = 0;
                    Integer goodsCount = 0;
                    for (Map<String, Object> sku : newSku) {
                        sku.put("isable", 0);
                        sku.put("goodsCount", null);
                        sku.put("integral", null);
                        sku.put("goodsId", integralGoodsId);
                        sku.put("originalCost", sku.get("originalCost"));
                        sku.put("goodsSkuId", sku.get("newSkuId"));
                        sku.put("originalPrice", sku.get("moneyAmount"));
                        sku.put("goodsSkuId", sku.get("newSkuId"));
                        sku.put("moneyAmount", null);
                        for (IntegralSku skuO : skuListO) {
                            if (skuO.getGoodsSkuId() != null && sku.get("newSkuId") != null &&
                                    skuO.getGoodsSkuId().toString().equals(sku.get("newSkuId").toString())) {
                                if (skuO.getIsable() == 1) {
                                    isAble = 1;
                                }
                                sku.put("isable", skuO.getIsable() == null ? 0 : skuO.getIsable());
                                sku.put("goodsCount", skuO.getGoodsCount());
                                sku.put("integral", skuO.getIntegral());
                                sku.put("moneyAmount", skuO.getMoneyAmount());
                            }
                        }
                        sku.remove("skuId");
                        integralGoodsDao.insertSku(sku);
                        if (!StringUtils.isEmpty(sku.get("goodsCount"))) {
                            goodsCount += Integer.valueOf(sku.get("goodsCount").toString());
                        }
                    }
                    params.remove("goodsStatus");
                    if (isAble == 0) {
                        params.put("invalidReason", "规格变更");
                        params.put("goodsStatus", 3);
                    }
                    params.put("goodsCount", goodsCount);
                } else {
                    //多规格变成单规格
                    params.remove("goodsStatus");
                    if (skuListO != null & skuListO.size() > 0) {
                        params.put("invalidReason", "规格变更");
                        params.put("goodsStatus", 3);
                    }
                }
                params.put("goodsId", integralGoodsId);
                params.put("originalPrice", params.get("moneyAmount"));
                params.put("moneyAmount", null);
                //单商品就直接同步
                integralGoodsDao.updateIntegralGoods(params);
            }
        }
    }

    @Override
    public PageInfo allianceShareList(Map<String, Object> params) {
        Integer pageIndex = (Integer) params.get("pageIndex");
        Integer pageSize = (Integer) params.get("pageSize");
        Boolean ifSales = (Boolean) params.get("ifSales");
        if (ifSales) {
            String customerId = merchantInfoDao.getCustomerIdByAdminId((String) params.get("adminId"));
            params.put("saler", customerId);
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Map<String, Object>> mapList = integralGoodsDao.allianceShareList(params);
        for (Map goodsInfo : mapList) {
            String quotaLevel = "";
            if (!StringUtils.isEmpty(goodsInfo.get("quotaLevel")) && !goodsInfo.get("quotaLevel").toString().equals("0")) {
                quotaLevel = "大于等于vip" + goodsInfo.get("quotaLevel").toString();
            }
            if (!StringUtils.isEmpty(goodsInfo.get("quotaVips"))) {
                String quotaVipNames1 = integralGoodsDao.getQuotaVipNames(goodsInfo.get("quotaVips").toString());
                if (!StringUtils.isEmpty(quotaLevel)) {
                    quotaLevel = quotaLevel + ";" + quotaVipNames1;
                } else {
                    quotaLevel = quotaVipNames1;
                }
            }
            goodsInfo.put("quotaVipNames", quotaLevel);
        }
        return new PageInfo<>(mapList);
    }

    @Override
    public List<Map<String, Object>> getMyShareGoodsList(Map<String, Object> param) {
        List<Map<String, Object>> myShareGoodsList = integralGoodsDao.getMyShareGoodsList(param);
        for (Map goodsInfo : myShareGoodsList) {
            String quotaLevel = "";
            if (!StringUtils.isEmpty(goodsInfo.get("quotaLevel")) && !goodsInfo.get("quotaLevel").toString().equals("0")) {
                quotaLevel = "大于等于vip" + goodsInfo.get("quotaLevel").toString();
            }
            if (!StringUtils.isEmpty(goodsInfo.get("quotaVips"))) {
                String quotaVipNames1 = integralGoodsDao.getQuotaVipNames(goodsInfo.get("quotaVips").toString());
                if (!StringUtils.isEmpty(quotaLevel)) {
                    quotaLevel = quotaLevel + ";" + quotaVipNames1;
                } else {
                    quotaLevel = quotaVipNames1;
                }
            }
            goodsInfo.put("quotaVipNames", quotaLevel);
        }
        return myShareGoodsList;
    }

    @Override
    public Integer saveSharing(Map<String, Object> param) {
        if (StringUtils.isEmpty(param.get("id"))) {
            throw GlobalException.build("参数错误");
        }
        return integralGoodsDao.saveSharing(param);
    }

    @Override
    public void addToMall(Map<String, Object> params) {
        Map<String, Object> merParam = new HashMap<>();
        merParam.put("orgId", params.get("orgId"));
        IntegralGoodsPo goodsInfo = integralGoodsDao.getGoodsInfo(Long.valueOf(params.get("goodsId").toString()));
        if (!StringUtils.isEmpty(goodsInfo.getBelongId())) {
            merParam.put("customerId", goodsInfo.getBelongId());
        }
        //  商家认证
        Map<String, Object> merInfo = newMallDao.getMerInfo(merParam);
        if (merInfo == null || !"2".equals(merInfo.get("checkStatus").toString())) {
            throw GlobalException.build("暂时无法添加，商家未进行认证");
        }
        integralGoodsDao.addToMallByGoodsId(params);
    }
}
